from django.shortcuts import render,HttpResponse

# 视图函数的语法：第一个参数都是request
# 都要返回一个HttpResponse或者HttpResponse的子类


# Create your views here.
def myhello(request):
    return HttpResponse("你好")


def index(request):
    return render(request,'index.html')
def index1(request):
    # return render(request,'index1.html',context={'name':'李四'})
    return render(request,'index1.html',{'name':'李四'})
def index2(request):
    return render(request,'index2.html', {'list1':[x for x in range(1,6)]})



def myform(request):
    return render(request,'myform.html')
# def myform2(request):
#     name = request.GET.get('name')
#     password = request.GET.get('password')
#     return HttpResponse("myform2,{},{}".format(name,password))

def myform2(request):
    name = request.POST.get('name')
    password = request.POST.get('password')
    return HttpResponse("myform2,{},{}".format(name,password))

def myform_post(request):
    return render(request,'myform_post.html')


def myform_single(request):
    if request.method == "GET":
        return render(request,'myform_single.html')
    if request.method == "POST":
        name = request.POST.get("name")
        password = request.POST.get("password")
        return HttpResponse("{},{}".format(name,password))


def upfile(request):
    if request.method =="GET":
        return render(request,'upfile.html')
    if request.method =="POST":
        file = request.FILES.get("file")
        print(file)
        print(type(file))
        with open("ok.jpg",'wb')as f:
            for chunk in file.chunks():
                f.write(chunk)
        return HttpResponse("kkk")



def show(request):
    print(request.get_raw_uri())
    host = request.get_host()
    print(host)
    # return HttpResponse("你的ip地址是:{}".format(host))
    if request.method == "GET":
        return render(request,'show.html')
    if request.method == "POST":
        name = request.POST.get("name")
        age = request.POST.get("age")
        tel = request.POST.get("tel")
        file = request.FILES.get("file")
        print(name,age,tel,file)
        print(type(file))
        with open("ok.jpg", 'wb')as f:
            for chunk in file.chunks():
                f.write(chunk)
            return HttpResponse("姓名:{},年龄:{},手机号：{},ip地址:{},头像照片:{}".format(name,age,tel,host,file))



from django.shortcuts import redirect,reverse

# def testfun(request):
#     return redirect("index2")  #通过views name跳转
#     return redirect("/school/index2") #通过硬编码 绝对路径 跳转
#     # return redirect("baidu.com")  识别为相对地址 本机的
#     return redirect("http://baidu.com") # 跳转百度
#     # [命名空间/appname]:路由名字
#
#     reverse()
#     indexpath = reverse("index")  #路由反向解析
#     print(indexpath)
#     return redirect(indexpath)



from .models import Teacher1,Student1
def testfun(request):
    s1 = Student1(name='张三')
    s1.save()
    #
    # bi = Teacher1(bookname='django',price=9.9,isbn='123456')
    # bi.save()
    #
    # Student1.objects.create(name='李四')


    # 批量创建对象
    # s1 = Student1(name='111')
    # s2 = Student1(name='222')
    # Student1.objects.bulk_create([s1,s2])

    # 查
    # s1 = Student.objects.get(id=2)
    # get查询出来的结果 只能有一个 否则就报错
    # s1 = Student.objects.get(name='张三')
    # print(s1)
    # print(s1.name)
    #
    # students = Student.objects.filter(name='李四')
    # for student in students:
    #     print(type(student))
    #     print(student.id)

# 名字包含
#     students = Student.objects.filter(name__contains='四')
    # students = Student.objects.filter(name__endswith='四')
    # students = Student.objects.filter(name__startswith='四')
    # print(type(students))
    # for student in students:
    #     print(student.id)

    # 改   根据查询出来的对象，更改其属性 save
    # s1 = Student.objects.get(id=1)
    # s1.name = 'rose'
    # s1.save()
    #
    # 一次更改多个
    # Student.objects.filter(name='222').update(name='马六')


    # 删除一个
    # s1 = Student.objects.get(id=1)
    # s1.delete()

    # 删除多个
    # Student.objects.filter(name='马六').delete()
    # return HttpResponse('okokok')


# def queryobj(request):
#     if request.method == 'GET':
#         return render(request,'queryobj.html ')


